clear all;clc
addpath(genpath('.'));  %添加工具箱路径
load Intensity
load dff
load bitstream

%% 确定同步起止+过滤邻近峰值-->id

% 这一步可以进一步优化：思路，寻求最佳的起止信号点
% Intensity=Intensity(1:1000);
% bitstream=bitstream(1:1000);
% Intensity(964:1300)=[];
% bitstream(964:1300)=[];
% dff(964:1300)=[];

[~,id]=findpeaks(Intensity,'minpeakheight',140);%300超参数
did=diff(id);[~,x]=find(did<10);id(x)=[];
% id=[10,id];
step=mean(diff(id));
figure,area(Intensity);
xlabel('Frame number')
ylabel('Synchronization signal intensity')
title('Detect synchronization signal in DCT domain')
figure,area(dff);
xlabel('Frame number')
ylabel('Authentication signal intensity')
title('Detect authentication signal in DFT domain')
%% 水印提取，先验n=64
flag=step/(64*5);
ss=round(flag*2.5);
l=length(id);
Cost_Map=[];
NC_Map=[];
%% 秘密信息生成
load key; rng(key);%%key为伪随机序列种子
msg = num2str(uint8(rand(1,64)));
msg=strrep(msg,' ','');%剔除空格
gt_w=mychar2double(msg)';

count=1;
%% 后处理
for a=2:8 %
    for b=-3:3
        for i=1:l-1
            watermark_zoom{i}=bitstream(id(i)+ss+a:id(i+1)+ss+b);%这里也不好定+5，+2，改进思路：1将DCT同步变短，且DFT水印变长
            extstream{i}=my_linear_interp(watermark_zoom{i},64);
        %     interp1(watermark_zoom{i},)
        end
        Cost_Map(a-1,b+4)=sum(abs(extstream{i}));
Final_w=zeros(1,64);
www=zeros(1,64);
%% 组间验证
for w=extstream
    Final_w=Final_w+w{1};
end
www(Final_w<0)=1;
www(Final_w>0)=0;



%% 算一算NC
diff_bit=sum(abs(www-gt_w));NC=1-diff_bit/length(www);
disp(['参数a=',num2str(a),' 参数b=',num2str(b),' 对应的NC值',num2str(NC),' 权重系数',num2str(sum(abs(extstream{i})))]);

W_Intensity(count)=sum(abs(extstream{i}));
Nc(count)=1-diff_bit/length(www);
count=count+1;
% NC_Map(a-2,b+3)=NC;
    end
end
% [~,id]=max(W_Intensity);
% disp(['The original authentication data :', num2str(gt_w)])
% disp(['The extract authentication data :', num2str(www)])
% 
% disp(['The NC metrics is : ',num2str(Nc(id))]);